using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;
using System.Windows.Forms;
using System.Workflow.ComponentModel.Compiler;
using System.Reflection;

namespace Devfarm
{
    class Program
    {
		[STAThread]
        static void Main(string[] args)
        {
			Devfarm.BootStrap.AddResolver();
        
            Dictionary<string, string> aliases = new Dictionary<string, string>();
            

            Dictionary<string, object> parameters = new Dictionary<string, object>();
            foreach(string arg in args)
            {
                string[] pair = arg.Split('=');
                if (pair.Count() != 2)
                    continue;

                string paramName = pair[0];
                string paramValue = pair[1];

                if (aliases[paramName] != null)
                    paramName = aliases[paramName];

                parameters.Add(paramName, paramValue);
                //log(string.Format("in [{0}]=[{1}]", paramName, paramValue));
            }

            Assembly visualizationLib = Assembly.Load("Devfarm.Common.UI");
            if (visualizationLib != null && aliases.Count > 0)
            {
                Type parametersType = visualizationLib.GetType("Devfarm.Runtime.Parameters.Parameters");
                object parametersHelper = Activator.CreateInstance(parametersType);
                MethodInfo mi = parametersType.GetMethod("Read", new Type[] { typeof(Type) });
                parameters = (Dictionary<string, object>)mi.Invoke(parametersHelper, new object[] { typeof(Devfarm.R1) });
            }

            Dictionary<string, object> outParams = Run(parameters);

            //foreach (string key in outParams.Keys)
                //log(string.Format("out [{0}]=[{1}]", key, outParams[key]));

            

			//Devfarm.Runtime.Parameters.Output.Display(typeof(object), obj);
        }
        static void log(string msg)
        {
            MessageBox.Show(msg);
        }

        static Dictionary<string, object> Run(Dictionary<string, object> parameters)
        {
            Dictionary<string, object> retVal = null;

            WorkflowRuntime workflowRuntime = new WorkflowRuntime();

            AutoResetEvent waitHandle = new AutoResetEvent(false);
            workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e) 
            {
                retVal = e.OutputParameters;
                waitHandle.Set(); 
            };
            workflowRuntime.WorkflowTerminated += delegate(object sender, WorkflowTerminatedEventArgs e)
            {
                log(e.Exception.Message);
                waitHandle.Set();
            };

            try
            {
                WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(Devfarm.R1), parameters);
                instance.Start();
                waitHandle.WaitOne();
            }
            catch (WorkflowValidationFailedException exp)
            {
                ValidationErrorCollection list = exp.Errors;
                foreach (ValidationError err in list)
                {
                    log(err.ErrorText);
                }
            }
            catch (Exception exc)
            {
                log(exc.Message);
            }

            return retVal;
        }
    }
}